package com.qiwo.webshow.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.DevelopDO;
import com.qiwo.webshow.service.DevelopService;
import com.qiwo.common.utils.Result;

/**
 * 
 * <pre>
 * 发展历程
 *  @author  chenwenchao
 *  @date 2018-11-04 21:28:17
 * </pre>
 * <small> 2018-11-04 21:28:17 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/develop")
public class DevelopController extends AdminBaseController {
	@Autowired
	private DevelopService developService;
	
	@GetMapping()
	@RequiresPermissions("webshow:develop:develop")
	String Develop(){
	    return "webshow/develop/develop";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("webshow:develop:develop")
	public Result<Page<DevelopDO>> list(DevelopDO developDTO){
        Wrapper<DevelopDO> wrapper = new EntityWrapper<DevelopDO>().orderBy("year", true);

        if(developDTO!=null){
        	if(developDTO.getYear()!=null ){
        		wrapper.eq("year",developDTO.getYear());
			}
		}
		Page<DevelopDO> page = developService.selectPage(getPage(DevelopDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("webshow:develop:add")
	String add(){
	    return "webshow/develop/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("webshow:develop:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DevelopDO develop = developService.selectById(id);
		model.addAttribute("develop", develop);
	    return "webshow/develop/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("webshow:develop:add")
	public Result<String> save( DevelopDO develop){
		developService.insert(develop);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("webshow:develop:edit")
	public Result<String>  update( DevelopDO develop){
		boolean update = developService.updateById(develop);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("webshow:develop:remove")
	public Result<String>  remove( Long id){
		developService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("webshow:develop:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		developService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
